﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class gold_eGoldWeekStocks : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
           
      
    }

    
    protected void ADD_Click(object sender, EventArgs e)
    {
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        string strId = this.hid_id.Value;
        string strDailyNews = this.dailynews.Text;
        string strDailyUP = this.dailyup.Text;
        string strDailyDown = this.dailydown.Text;
        DateTime dtDate=Convert.ToDateTime(this.date.Text);
        int iDayofWeek=(int)dtDate.DayOfWeek;
        CMSModel.eGold_WeekStocks news;


        if (strId == "")
        {
            try
            {
                news = dal.eGold_WeekStocks.First(x => x.date == dtDate);
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('和以后数据重复,请确认日期');", true);
            }
            catch (Exception ex)
            {
                CMSModel.CMSEntities dal1 = DAL.DBManager.GetCmsEmtity();
                var c= new CMSModel.eGold_WeekStocks();
                 c.dailynews = strDailyNews.Replace("\r\n", "<br>");
                   c. dailyup = strDailyUP;
                    c.dailydown = strDailyDown;
                    c.date = dtDate;
                    c.dayofweek = iDayofWeek;
                    c.createdate = System.DateTime.Now;
                    dal1.eGold_WeekStocks.AddObject(c);
                
               
                
                dal1.SaveChanges();
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('新增成功');", true);
            } 
            
            
        }
        else
        {
            int iId = int.Parse(strId);
            news = dal.eGold_WeekStocks.First(x => x.id == iId);
            news.dailynews = strDailyNews.Replace("\r\n", "<br>");
            news.dailyup = strDailyUP;
            news.dailydown = strDailyDown;
            dal.SaveChanges();
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('修改成功');", true);
        }
          
        
    }


    protected void search_Click(object sender, EventArgs e)
    {
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        DateTime dtDate = Convert.ToDateTime(this.search_date.Text);
        int iDayofWeek =(int)dtDate.DayOfWeek;
        DateTime dtStartDate=dtDate.AddDays(1-iDayofWeek);
        DateTime dtEndDate = dtDate.AddDays(5-iDayofWeek);   
        var news = dal.eGold_WeekStocks.Select(x => new {x.id,x.dayofweek ,x.dailynews,x.dailyup,x.dailydown,x.date });
        news = news.Where(x => x.date >= dtStartDate && x.date <= dtEndDate);
        Repeater1.DataSource = news;
        Repeater1.DataBind();
    }
    

    protected void Delete_Click(object sender, EventArgs e)
    {
        int id = int.Parse(this.hid_id.Value);

        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        CMSModel.eGold_WeekStocks news = dal.eGold_WeekStocks.First(x => x.id == id);
        dal.DeleteObject(news);
        dal.SaveChanges();
    }
}